<?php
global $_W, $_GPC;
$uid = $_GPC['uid'];
$lessonid = $_GPC['lid'];
if (empty($uid)) {
    return $this->getresult(1, '请先登录', array('code' => 3001, 'msg' => '请先登录'));
}
if (empty($lessonid)) {
    return $this->getresult(1, '请先选择课程', array('code' => 3001, 'msg' => '请先选择课程'));
}
/***校验token开始***/
if ($uid) {
    $token = $_GPC['token'];
    if (empty($token)) {
        return $this->getresult(1, '请重新登录', ['code' => 4001, 'msg' => '请重新登录']);
    }
    $token_result = $this->checktoken($uid, $token);
    if ($token_result == 1002) {
        return $this->getresult(1, '请重新登录', ['code' => 4002, 'msg' => '请重新登录']);
    } elseif ($token_result == 1003) {
        return $this->getresult(1, '请重新登录', ['code' => 4003, 'msg' => '请重新登录']);
    } elseif ($token_result == 1001) {
        //  更新token
        pdo_update($this->t_user_token, array('createtime' => time()), array('uid' => $uid, 'token' => $token, 'weid' => $_W['uniacid']));
    }
}
/***校验token结束***/
$weid = $_W['uniacid'];
//章节列表
$contion = " weid = :weid AND lessonid = :lessonid AND status = :status ";
$params = [':weid' => $weid, ':lessonid' => $lessonid, ':status' => 1];
$sonList = pdo_fetchall("select * from " . tablename('goouc_class_son') . "  where weid = :weid AND lessonid = :lessonid AND status = :status  order by orders desc", $params);
//课程总章节数
$totalSon = pdo_fetchcolumn("select count(*) as totalSon  from " . tablename('goouc_class_son') . "  where " . $contion, $params);
//会员学习总时长（只统计观看完的）统计百分比
$paramsStady = [':weid' => $weid, ':lid' => $lessonid, ':uid' => $uid];
$studytime = pdo_fetchcolumn("select sum(s.vediotime) as studyTime  from " . tablename('goouc_class_stady') . " cs LEFT JOIN " . tablename('goouc_class_son') . " s ON cs.sonid = s.id LEFT JOIN " . tablename('goouc_class_lesson') . " l ON s.lessonid = l.id where cs.weid = :weid AND cs.uid = :uid AND l.id = :lid", $paramsStady);
//进度条
$studySon = pdo_fetchcolumn("select COUNT(*) as studySon FROM " . tablename('goouc_class_stady') . " cs LEFT JOIN " . tablename('goouc_class_son') . " s ON cs.sonid = s.id LEFT JOIN " . tablename('goouc_class_lesson') . " l ON s.lessonid = l.id where cs.weid = :weid AND cs.uid = :uid AND l.id = :lid AND s.status = :status", array(':weid' => $weid, ':uid' => $uid, ':lid' => $lessonid, ':status' => 1));
if ($totalSon) {
    $jindu = floor(($studySon / $totalSon) * 100) / 100;
} else {
    $jindu = 0;
}

//星星总数
$totalstar = 3 * $totalSon;
//做题获得星星总数
$userstar = pdo_fetchcolumn("select sum(num) as userstar from " . tablename('goouc_class_user_star') . " WHERE weid = :weid AND lessonid = :lessonid AND uid = :uid AND status = :status", array(':weid' => $weid, ':uid' => $uid, ':lessonid' => $lessonid, ':status' => 1));
foreach ($sonList as $k => &$v) {
    //判断有没有习题
    $havepractice = pdo_get('goouc_class_practice', array('weid' => $weid, 'sonid' => $v['id'], 'status' => 1, 'is_delete' => 1), array('id'));
    $v['havepractice'] = $havepractice['id'];
    if (empty($havepractice)) {
        unset($sonList[$k]);
    }
    if ($v['sontype'] == 1 || $v['sontype'] == 4) {
        $v['livetype'] = 1;//直播课程
        if ($v['starttime'] > time()) {
            $v['livestatus'] = 2;//直播未开始
        } elseif (($v['starttime'] + 20 * 60) < time()) {
            //把章节下架（直播开始时间超了20分钟还未开始）
            pdo_update('goouc_class_son', array('status' => 2), array('id' => $v['id']));
        } else {
            $v['livestatus'] = 1;//直播开始
        }
    } else {
        $v['livetype'] = 0;//录播
    }
    //判断习题是否做过
    $checkPractice = pdo_fetchcolumn("select id from " . tablename('goouc_class_practice_record') . " WHERE weid = :weid AND sonid = :sonid AND uid = :uid AND status = :status limit 1", array(':weid' => $weid, ':uid' => $uid, ':sonid' => $v['id'], ':status' => 1));
    if ($checkPractice) {
        $v['practicestatus'] = 1;//习题已经做过
    } else {
        $v['practicestatus'] = 2;//习题没做过
    }
    //该章节星星数量
    $getstar = pdo_fetchcolumn("select num from " . tablename('goouc_class_user_star') . " WHERE weid = :weid AND sonid = :sonid AND uid = :uid AND status = :status ", array(':weid' => $weid, ':uid' => $uid, ':sonid' => $v['id'], ':status' => 1));
    $v['getstar'] = empty($getstar) ? 0 : $getstar;
    //判断是不是可以做习题
    $checkstudy = pdo_fetchcolumn("select id FROM " . tablename('goouc_class_stady') . " where weid = :weid AND uid = :uid AND sonid = :sonid", array(':weid' => $weid, ':uid' => $uid, ':sonid' => $v['id']));
    $v['dostatus'] = empty($checkstudy) ? 2 : 1;//1可以做 2不可以做

}

$sonListNew = [];

foreach ($sonList as $nv) {
    $sonListNew[] = $nv;
}

$lesson = pdo_get('goouc_class_lesson', array('weid' => $weid, 'id' => $lessonid), array('is_only', 'fic_study_num', 'study_num'));

$data = [
    'sonlist' => $sonListNew,
    'studytime' => $studytime,//会员学习总时长
    'jindu' => $jindu,
    'is_only' => $lesson['is_only'],//1为单节课  2为系列课
    'studynum' => $lesson['fic_study_num'] + $lesson['study_num'],
    'totalstar' => $totalstar,
    'userstar' => empty($userstar) ? 0 : $userstar
];
return $this->getresult(0, '课程章节数据', ['code' => 200, 'lessoninfo' => $data]);